<?php

declare(strict_types=1);

namespace App\Controller;

use App\Model\Banner;
use Hyperf\Redis\Redis;
use Hyperf\HttpServer\Contract\RequestInterface;
use Psr\Container\ContainerExceptionInterface;
use Psr\Container\NotFoundExceptionInterface;
use Psr\Http\Message\ResponseInterface;
use RedisException;
use Trait\ResponseMsg;
use Util\Common;
use App\Model\Normal;

/**
 * 首页
 */
class NormalController extends AbstractController
{
    use ResponseMsg;

    protected Redis $redis;
    protected Normal $normal;

    public function __construct()
    {
        $this->redis = container()->get(Redis::class);
        $this->normal = container()->get(Normal::class);
    }

    /**前端所需通用数据
     * @return ResponseInterface
     * @throws ContainerExceptionInterface
     * @throws NotFoundExceptionInterface|RedisException
     */
    public function getNormal(): ResponseInterface
    {
        $normal = $this->normal->get_redis_array(Common::SET_WEB_NORMAL_WEN);//获取seo通用数据
        return $this->success($normal);
    }

    /**获取轮播图
     * @param RequestInterface $request
     * @return ResponseInterface
     * @throws ContainerExceptionInterface
     * @throws NotFoundExceptionInterface
     * @throws RedisException
     */
    public function getBanner(RequestInterface $request): ResponseInterface
    {
        $params = $request->all();
        $list =$this->normal->get_redis_array(Common::SLICK_IMAGE.$params['type']);
        if(!$list){
            $where = Common::buildWhere($params);
            $list = Banner::where($where)
                ->where('is_del', Common::DEL_SHOW)
                ->get(['id', 'url', 'type', 'addr']);
            $this->redis->setex(Common::SLICK_IMAGE.$params['type'],60*60*24*7,json_encode($list));
        }
        return $this->success($list);
    }
}
